﻿using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Common.Helpers
{
    internal class PlayerHelper
    {
        public static float MinimumRunSpeed(Player player)
        {
            float max = (player.accRunSpeed + player.maxRunSpeed) / 2f;
            if (player.windPushed && (!player.mount.Active || player.velocity.Y != 0f))
            {
                float wind = Math.Sign(Main.windSpeedCurrent) * 0.07f;
                if (Math.Abs(Main.windSpeedCurrent) > 0.5f)
                {
                    wind *= 1.37f;
                }
                if (player.velocity.Y != 0f)
                {
                    wind *= 1.5f;
                }
                if (player.controlLeft || player.controlRight)
                {
                    wind *= 0.8f;
                }
                if (Math.Sign(player.direction) != Math.Sign(wind))
                {
                    max -= Math.Abs(wind) * 40f;
                }
            }

            return max;
        }
        public static int DropInventoryItem(Player player, int slot, int noGrabDelay = 100)
        {
            Item item = player.inventory[slot];
            if (item == null || item.IsAir)
            {
                return -1;
            }
            int itemIdx = Item.NewItem(Entity.GetSource_NaturalSpawn(), (int)player.position.X, (int)player.position.Y, player.width, player.height, item.type, item.stack, false, -1, false, false);
            Item protoNewItem = Main.item[itemIdx];
            item.position.X = protoNewItem.position.X;
            item.position.Y = protoNewItem.position.Y;
            Main.item[itemIdx] = item;
            player.inventory[slot] = new Item();
            if (slot == 58 && player.whoAmI == Main.myPlayer)
            {
                Main.mouseItem = new Item();
            }
            item.velocity.Y = Main.rand.Next(-20, 1) * 0.2f;
            item.velocity.X = Main.rand.Next(-20, 21) * 0.2f;
            item.noGrabDelay = noGrabDelay;
            item.favorited = false;
            item.newAndShiny = false;
            item.playerIndexTheItemIsReservedFor = player.whoAmI;
            if (Main.netMode != NetmodeID.SinglePlayer && noGrabDelay > 0)
            {
                item.ownIgnore = player.whoAmI;
                item.ownTime = noGrabDelay;
            }
            Recipe.FindRecipes();
            if (Main.netMode != NetmodeID.SinglePlayer)
            {
                float delay = noGrabDelay > 0
                    ? 0f
                    : 1f;
                NetMessage.SendData(MessageID.SyncItem, -1, -1, null, itemIdx, delay, 0f, 0f, 0, 0, 0);
            }
            return itemIdx;
        }
        public static bool IsFlying(Player player)
        {
            if (player.dead)
            {
                return false;
            }

            bool wingFly = !player.pulley && player.grappling[0] == -1 && !player.tongued &&
                player.controlJump && player.wingTime > 0f && (
                player.wingsLogic > 0 && !player.GetJumpState<CloudInABottleJump>().Enabled && player.jump == 0 && player.velocity.Y != 0f ||
                player.controlDown && (
                    player.wingsLogic == 22 ||
                    player.wingsLogic == 28 ||
                    player.wingsLogic == 30 ||
                    player.wingsLogic == 32 ||
                    player.wingsLogic == 29 ||
                    player.wingsLogic == 33 ||
                    player.wingsLogic == 35 ||
                    player.wingsLogic == 37)
                
            );

            bool rocketFly = (player.wingTime == 0f || player.wingsLogic == 0) &&
                player.rocketBoots > 0 &&
                player.controlJump &&
                player.canRocket &&
                player.rocketRelease &&
                !player.GetJumpState<CloudInABottleJump>().Enabled &&
                player.rocketTime > 0;

            return wingFly || rocketFly;
        }
    }
}

